using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace Easily
{
    ///==============================================================================
    /// <summary>
    /// 功能说明：是控件的基类，供GeneralIcon等使用。提供通用的方法。
    /// </summary>
    ///==============================================================================
    public class Icon
    {
        #region Icon类的字段
        ///==============================================================================

        //依据控件连接点个数分类的控件类型，当前工程仅使用了通用控件，其他为以前版本使用
        public enum LinkIconType
        {
            GeneralIcon,       //通用控件,一个入点，一个出点
            NULL,              //无
        }
        public LinkIconType IconType;//控件的类型
        public string ModuleName;//控件名字
        //【2021/1/30 添加IconName与ModuleName区分】
        public string IconName;
        public int IconArrNum;//控件在控件数组中的下标
        public int IconDbNum;//控件在数据库中的序号
        public PictureBox IconPicBox;//控件的图片
        public PictureBox MoveToIconPicBox;//鼠标移动到控件上时显示的图片
        public int IconLeftDotNum; //控件图标的左侧的Dotsize个数
        public int IconTopDotNum; //控件图标的上侧的Dotsize个数（在鼠标点击位置为中心，这两个变量决定图标左上角的位置）
        public bool MoveIcon; //左键按住图标控件标记 20151207
        public bool UpIcon;//拖动图标控件标记20151207 

        ///==============================================================================
        #endregion

        #region Icon类的方法
        ///==============================================================================

        #region LoadCtrlImage: Icon类的构造函数
        /// <summary>
        /// Icon类的构造函数
        /// </summary>
        ///==============================================================================
        public Icon()
        {
            //1. 字段初始化
            IconType = LinkIconType.NULL;
            ModuleName = "";
            IconArrNum = -1;
            IconDbNum = -1;
            MoveIcon = false;
            UpIcon = false;
            IconLeftDotNum = 0;
            IconTopDotNum = 0;

            //2. 控件图片信息
            //(1) 控件图片
            IconPicBox = new PictureBox();
            this.IconPicBox.Height = (int)(Globaldefine.DotSize * 50);//50
            this.IconPicBox.Width = (int)(Globaldefine.DotSize * 120);//120
            this.IconPicBox.Cursor = System.Windows.Forms.Cursors.Hand;
            this.IconPicBox.Refresh();
            this.IconPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
            //(2) 鼠标移动到控件上的图片信息
            MoveToIconPicBox = new PictureBox();
            this.MoveToIconPicBox.Height = (int)(Globaldefine.DotSize * 50);
            this.MoveToIconPicBox.Width = (int)(Globaldefine.DotSize * 120);
            this.MoveToIconPicBox.Cursor = System.Windows.Forms.Cursors.Hand;
            this.MoveToIconPicBox.Refresh();
            this.MoveToIconPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
            //3. 事件定义
            this.MoveToIconPicBox.MouseMove += new MouseEventHandler(MoveToIconPicBox_MouseMove);
        }
        //绘制拖动控件时的虚线框
        public void DrawRect(int x, int y, BufferedGraphics bg)
        {
            Pen pen = new Pen(Color.Black, Globaldefine.DotSize * 2);//创建画笔
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//设置线的样式为虚线
            bg.Graphics.DrawRectangle(pen, x - this.IconPicBox.Width / 2, y - this.IconPicBox.Height / 2, this.IconPicBox.Width, this.IconPicBox.Height);//绘制虚线框
        }

        ///==============================================================================
        #endregion

        ///==============================================================================
        #endregion

        #region Icon类的事件
        ///==============================================================================

        /// <summary>
        /// 实现拖动控件功能 2015-5-16 【GZF】
        /// </summary>
        public void MoveToIconPicBox_MouseMove(object sender, MouseEventArgs e)
        {
            
            try       //移动控件
            {
                if (this.MoveIcon && e.Button == MouseButtons.Left && (!((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).BFirstClickOutDot))
                {
                    Graphics g = ((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).CreateGraphics();
                    BufferedGraphicsContext context = BufferedGraphicsManager.Current;
                    BufferedGraphics bg = context.Allocate(g, ((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).ClientRectangle);

                    ((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).ReDraw(bg);
                    DrawRect(e.X + this.IconPicBox.Left, e.Y + this.IconPicBox.Top, bg);//画虚线框

                    bg.Render();
                    bg.Dispose();
                    bg = null;

                    this.UpIcon = true;
                }
                else if (((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).BFirstClickOutDot)
                {
                    ((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).lineEndX = e.X + this.MoveToIconPicBox.Left;
                    ((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).lineEndY = e.Y + this.MoveToIconPicBox.Top;
                    ((frmGraphicProgrammer)(((PictureBox)sender).GetContainerControl())).ReDraw();
                    this.UpIcon = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        ///==============================================================================
        #endregion
    }
}
